<?php

class ContactController extends Zend_Controller_Action
{


    public function init()
    {
    }

    public function indexAction()
    {
       $this->view->title = 'Contact Us - Welcome to Rosemoore Lake';

        if(isset($_POST['action'])){
          try{
               
                 require_once "Mail.php";

                 require_once "Mail/mime.php";

                $config = Zend_Registry::get('mail_config');
       

                 $from = $_POST['name'] ."<".$_POST['mail'].">";
                 $to = "nirensinha@gmail.com";//$this->config->mail->to;
                 $subject = $_POST['subject'];
                 $body =  $_POST['emailBody'];

//                 $name_of_uploaded_file =  basename($_FILES['uploaded_file']['name']);
//
//
//                 $size_of_uploaded_file = $_FILES["uploaded_file"]["size"]/1024;
//
//                 if($name_of_uploaded_file !="" && $size_of_uploaded_file ==0){
//                     $this->view->message="<center><img src='images/cross.png'/>&nbsp;&nbsp;&nbsp;<font color='red'>Invalid file format.</font></center>";
//                }elseif($size_of_uploaded_file > $config->mail->maxsize){
//                     $this->view->message="<center><img src='images/cross.png'/>&nbsp;&nbsp;&nbsp;<font color='red'>The maximum size of file that can  be uploaded is 5 MB</font></center>";
//                  }else{

                         $host = $config->mail->host;
                         $username = $config->mail->username;
                         $password =$config->mail->password;
                         $port =$config->mail->port;


//                         if($size_of_uploaded_file !=0){
//                                $path_of_uploaded_file = $config->mail->tempfolder . $name_of_uploaded_file;
//                                $tmp_path = $_FILES["uploaded_file"]["tmp_name"];
//
//                                if(is_uploaded_file($tmp_path))
//                                    {
//                                      if(!copy($tmp_path,$path_of_uploaded_file))
//                                      {
//                                              $this->view->message="<center><img src='images/cross.png'/>&nbsp;&nbsp;&nbsp;<font color='red'> Failed to send the Message.</font></center>";
//                                      }
//                                    }
//                         }

                        $message = new Mail_mime();
                        $message->setTXTBody($body);
//                          if($size_of_uploaded_file !=0){
//                                 $message->addAttachment($path_of_uploaded_file);
//                          }
                        $body = $message->get();
                        $extraheaders = array("From"=>$from, "Subject"=>$subject,"To"=>$to);
                        $headers = $message->headers($extraheaders);
                        $mail = Mail::factory("mail");


                         $smtp = Mail::factory('smtp',
                           array ('host' => $host,
                             'auth' => true,
                             'port'=>$port,
                             'username' => $username,
                             'password' => $password));

                         $mail = $smtp->send($to, $headers, $body);

                         if($size_of_uploaded_file !=0){
                            unlink($path_of_uploaded_file);
                         }

                         if (PEAR::isError($mail)) {
                           $this->view->message ="<center><img src='images/cross.png'/>&nbsp;&nbsp;&nbsp;<font color='red'> Failed to send the Message.</font></center>";
                          } else {
                            $this->view->message ="<center><img src='images/check.png'/>&nbsp;&nbsp;&nbsp<font color='green'> Message successfully sent! </font></center>";
                            }
//                         }
                }catch(Exception $e){
                    Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
                     $this->view->message ="<center><img src='images/cross.png'/>&nbsp;&nbsp;&nbsp;<font color='red'> Failed to send the Message.</font></center>";
                }
         }
    }

    
}